package com.shigen.demo.base;

import java.util.concurrent.TimeUnit;

/**
 * 多线程卖票的demo
 *
 * @author shigenfu
 * @date 2022/5/30 8:07 下午
 */
public class TicketWithThreadDemo {


  public static void main(String[] args) {
    saleTicket saleTicket = new saleTicket();
    new Thread(saleTicket, "aa").start();
    new Thread(saleTicket, "bb").start();
    new Thread(saleTicket, "cc").start();
  }
}

class saleTicket implements Runnable {

  private static  Integer number = 100;
  // private Object object = new Object();

  @Override
  public  void run() {
    while (true) {
      synchronized (this) {
        notify();

        if (number > 0) {
          try {
            TimeUnit.MILLISECONDS.sleep(10);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          System.out.println(Thread.currentThread().getName() + "售出 " + --number);
          try {
            wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        } else {
          System.err.println("票已经售完");
          break;
        }
      }
    }
  }
}
